package com.anan.controller;

import com.anan.constants.Constants;
import com.anan.pojo.dto.ImgDTO;
import com.anan.pojo.dto.ShowImgsDTO;
import com.anan.pojo.vo.ImgInfosVO;
import com.anan.service.ImgResourceService;
import com.anan.utils.ComResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author anan
 * @date 2023/05/08/10:03
 **/
@Slf4j
@RestController
@RequestMapping("salvage/imgUrls")
public class ImgResourceController {

    @Resource
    ImgResourceService imgResourceService;

    @PostMapping("/addImg")
    public ComResult addImg(@RequestBody ImgDTO imgDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

        if (null == imgDTO || null == imgDTO.getImgInfosBOs() || imgDTO.getImgInfosBOs().size() == 0) {
            comResult.setMessage("未选择图片");
        }

        boolean flag = imgResourceService.addImg(imgDTO);

        if (flag) {
            comResult.setCode(Constants.RET_CODE.SUCCESS);
            comResult.setMessage("上传成功！");
        } else {
            comResult.setMessage("上传失败！");
        }

        return comResult;
    }

    @PostMapping("/getImg")
    public ComResult getImg(@RequestBody ShowImgsDTO showImgsDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

        if (null == showImgsDTO || null == showImgsDTO.getImgType() || null == showImgsDTO.getRelateId()) {
            log.info("=======参数不全=======");
            return null;
        }

        ImgInfosVO imgInfosVO = imgResourceService.getImgInfos(showImgsDTO);

        if (null != imgInfosVO) {
            comResult.setData(imgInfosVO);
            comResult.setCode(Constants.RET_CODE.SUCCESS);
        } else {
            comResult.setMessage("获取图片失败");
        }
        return comResult;
    }
}
